<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 收费订单管理
 *
 * @icon fa fa-circle-o
 */
class FeeOrder extends Backend
{

    /**
     * FeeOrder模型对象
     * @var \app\common\model\FeeOrder
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\FeeOrder;
        $this->view->assign("isPayList", $this->model->getIsPayList());
        $this->view->assign("isRefundList", $this->model->getIsRefundList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->has_log = (bool)(\app\common\model\FeeLog::where('order_id', $row->id)->find());
//                $row->visible(['id','order_sn','nickname','total_amount','guarantee_fee','settle_fee','device_fee','manage_fee','is_pay','is_refund','refund_amount','paytime','refundtime','deduct_fee']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function state($order_id = null)
    {
        $row = $this->model->get($order_id);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if ($row->state != 0) {
            $this->error('审核失败');
        }

        $state = $this->request->request('state');

        if ($state == 1) {
            $row->refund_amount = round($row->guarantee_fee - $row->deduct_fee, 2);
            $row->is_refund = 1;
            $row->refundtime = time();
        }

        $row->state = $state;
        $row->statetime = time();
        $row->save();

        $this->success('审核成功');
    }

}
